import time
from telnetlib import EC

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import ChatDriver

class ContactList:
    url=""
    driver=""
    def __init__(self):
        self.driver = ChatDriver.driver

    def ContactTestByLogin(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.tab > div.tab-friend").click()
        #检查元素
        #用户名称
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.user")
        #搜索框
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.search")
        #搜索按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client - container > div > div.l")
        #聊天按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.tab > div.tab-session")
        #联系人列表按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.tab > div.tab-friend")
        #联系人姓名
        self.driver.find_element(By.CSS_SELECTOR,"#friend-list > li:nth-child(1)")

        #获取联系人名称
        actal1=self.driver.find_element(By.CSS_SELECTOR,"#friend-list > li:nth-child(1)").text
        self.driver.find_element(By.CSS_SELECTOR,"#friend-list > li:nth-child(1)").click()
        actal2=self.driver.find_element(By.CSS_SELECTOR,"#session-list > li.selected > h3").text
        assert actal1 == actal2
        print("Before printing actal1")
        print(f"{actal1}")
        print("Before printing actal2")
        print(f"{actal2}")
        # 添加屏幕截图
        ChatDriver.getScreeShot()
    #未登录
    def ContactTestByNotLogin(self):
        newurl = "http://127.0.0.1:8080/client.html"
        self.driver.get(newurl)
        time.sleep(3)
        # 添加屏幕截图
        ChatDriver.get_alert_image()
        time.sleep(3)
        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "当前用户未登录!"
        alert.accept()
        # 回到登录界面
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 切换到警告框
        alert = self.driver.switch_to.alert
        alert.accept()
Login = ContactList()
# Login.ContactTestByLogin()

